package com.hefu.virtuallife.ui.ticket;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hefu.virtuallife.R;
import com.hefu.virtuallife.bean.TrainTicketBean;
import com.hefu.virtuallife.ui.VoiceActivity;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AutoBookingActivity extends VoiceActivity {

    @BindView(R.id.tab)
    TabLayout tab_layout;
    @BindView(R.id.viewPager)
    ViewPager vp;
    @BindView(R.id.tv_ticketinfo)
    TextView tv_ticketinfo;
    @BindView(R.id.rl_ticketinfo)
    RelativeLayout rl_ticketinfo;

    private boolean isDateInited = false;

    private List<TicketInfoListFragment> fragments;

    private int tab_icons[] = {R.drawable.icon_train, R.drawable.icon_highspeed_rail, R.drawable.icon_plane};
    private int tab_icon_sels[] = {R.drawable.icon_train_sel, R.drawable.icon_highspeed_rail_sel, R.drawable.icon_plane_sel};

    @Override
    protected void onResume() {
        super.onResume();
        if (!isDateInited){
            showDialog();
        }
    }

    @Override
    protected int setLayoutId() {
        return R.layout.activity_auto_booking;
    }

    @Override
    protected void initVariables(@Nullable Bundle savedInstanceState) {
        fragments = new ArrayList<>();
        fragments.add(TicketInfoListFragment.newInstance("1"));
        fragments.add(TicketInfoListFragment.newInstance("2"));
        fragments.add(TicketInfoListFragment.newInstance("3"));
        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

            @Override
            public int getCount() {
                return fragments.size();
            }

            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }
        };
        vp.setOffscreenPageLimit(3);
        vp.setAdapter(adapter);
        tab_layout.setupWithViewPager(vp);

        tab_layout.setTabMode(TabLayout.MODE_FIXED);
        TabLayout.Tab tab1 = tab_layout.newTab();
        TabLayout.Tab tab2 = tab_layout.newTab();
        TabLayout.Tab tab3 = tab_layout.newTab();
        tab_layout.addTab(tab1);
        tab_layout.addTab(tab2);
        tab_layout.addTab(tab3);
        tab_layout.setupWithViewPager(vp);
        for(int i=0;i<tab_icons.length;i++){
            tab_layout.getTabAt(i).setIcon(tab_icons[i]);
        }
        tab_layout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                tab.setIcon(tab_icon_sels[position]);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                tab.setIcon(tab_icons[position]);
                rl_ticketinfo.setVisibility(View.GONE);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
                int position = tab.getPosition();
                tab.setIcon(tab_icon_sels[position]);
            }
        });
    }

    @OnClick(R.id.img_return)
    protected void onFinishClick() {
        finish();
    }

    @OnClick(R.id.img_personal)
    protected void onPersonalClick() {
        Intent intent = new Intent(mContext, BookingHistoryActivity.class);
        mContext.startActivity(intent);
    }

    @Override
    protected void uiProcess() {
        tab_layout.getTabAt(1).select();
        if (!isDateInited){
            isDateInited = true;
            showTemporaryInfo();
            fillData();
        } else {
            rl_ticketinfo.setVisibility(View.GONE);

            Calendar calendar=Calendar.getInstance();
            int month=calendar.get(Calendar.MONTH)+1;
            int date=calendar.get(Calendar.DATE);
            int hour = calendar.get(Calendar.HOUR);
            int minute = calendar.get(Calendar.MINUTE);

            String dateStr = month + "月" + date + "号";
            String timeStr = hour + ":" + minute;
            String info = getResources().getString(R.string.ticket_book_result);
            String formatStr = String.format(info, dateStr, timeStr);

            LayoutInflater layoutInflater = LayoutInflater.from(mContext);
            View ticket_layout = layoutInflater.inflate(R.layout.layout_dialog_ticket_result, null);

            TextView tv = ticket_layout.findViewById(R.id.tv_remind);
            tv.setText(formatStr);

            AlertDialog dialog = new AlertDialog.Builder(mContext).setView(ticket_layout).create();
            dialog.setCancelable(true);
            dialog.setCanceledOnTouchOutside(true);
            dialog.show();
        }
    }

    @SuppressLint("StringFormatMatches")
    public void showTemporaryInfo(){
        rl_ticketinfo.setVisibility(View.VISIBLE);
        Calendar calendar=Calendar.getInstance();
        int month=calendar.get(Calendar.MONTH)+1;
        int date=calendar.get(Calendar.DATE);
        String dateStr = month + "月" + date + "号";
        String info = getResources().getString(R.string.ticket_search_result);
        tv_ticketinfo.setText(String.format(info, dateStr));
    }

    private void fillData(){
        List<TrainTicketBean> list = getData();
        for (TicketInfoListFragment fragment : fragments){
            fragment.addData(list);
        }
    }

    private void showDialog() {
        Dialog dialog = new Dialog(this, R.style.transDialog);
        dialog.setCancelable(true);
        dialog.setContentView(R.layout.layout_dialog_hotel);
        dialog.show();

    }

    public List<TrainTicketBean> getData() {
        List<TrainTicketBean> list = new ArrayList<TrainTicketBean>();
        TrainTicketBean bean1 = new TrainTicketBean("D352", "06:11","14小时16分钟","20:27","一等：无","二等：无","无座：3张","上海虹桥","成都东");
        TrainTicketBean bean2 = new TrainTicketBean("D636", "06:33","14小时28分钟","21:01","一等：无","二等：无","无座：有","上海虹桥","成都东");
        TrainTicketBean bean3 = new TrainTicketBean("D2206", "06:58","15小时3分钟","22:01","一等：无","二等：无","无座：有","上海虹桥","成都");
        TrainTicketBean bean4 = new TrainTicketBean("G1974", "07:17","11小时8分钟","18:25","商务：无","一等：无","二等：无","上海虹桥","成都东");
        TrainTicketBean bean5 = new TrainTicketBean("D952", "08:30","12小时58分钟","21:28","二等：无","动卧：无","无座：无","上海","成都东");

        list.add(bean1);
        list.add(bean2);
        list.add(bean3);
        list.add(bean4);
        list.add(bean5);

        return list;
    }
}
